package com.tencent.qcloud.tim.uikit.modules.conversation;

import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.tencent.qcloud.tim.uikit.R;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationIconView;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo;
import com.tencent.qcloud.tim.uikit.modules.conversation.interfaces.IConversationAdapter;
import com.tencent.qcloud.tim.uikit.modules.conversation.interfaces.IConversationProvider;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfoUtil;
import com.tencent.qcloud.tim.uikit.utils.DateTimeUtil;
import com.tencent.qcloud.tim.uikit.utils.ScreenUtil;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ConversationListAdapter extends IConversationAdapter {

    private boolean mHasShowUnreadDot = true;
    private int mItemAvatarRadius = ScreenUtil.getPxByDp(5);
    private int mTopTextSize;
    private int mBottomTextSize;
    private int mDateTextSize;
    private List<ConversationInfo> mDataSource = new ArrayList<>();
    private ConversationListLayout.OnItemClickListener mOnItemClickListener;
    private ConversationListLayout.OnItemLongClickListener mOnItemLongClickListener;

    public ConversationListAdapter() {
    }

    public ConversationListAdapter(List<ConversationInfo> data) {
        super(data);
        addItemType(ConversationInfo.TYPE_CUSTOM, R.layout.conversation_custom_adapter);
        addItemType(ConversationInfo.TYPE_COMMON, R.layout.conversation_adapter);
    }


    public void setOnItemClickListener(ConversationListLayout.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(ConversationListLayout.OnItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    public void setDataProvider(IConversationProvider provider) {
        mDataSource = provider.getDataSource();
        // KLog.e("ConversationManagerKit mDataSource =  d"+mDataSource.get(1).toString());
        if (provider instanceof ConversationProvider) {
            provider.attachAdapter(this);
        }
        setNewInstance(mDataSource);
        notifyDataSetChanged();
    }


    @Override
    protected void convert(@NotNull final BaseViewHolder holder, final ConversationInfo conversation) {
        switch (holder.getItemViewType()) {
            case ConversationInfo.TYPE_CUSTOM: {

                LinearLayout leftItemLayout = holder.getView(R.id.item_left);
                ConversationIconView conversationIconView = holder.getView(R.id.conversation_icon);
                TextView titleText = holder.getView(R.id.conversation_title);
                TextView messageText = holder.getView(R.id.conversation_last_msg);
                TextView timelineText = holder.getView(R.id.conversation_time);
                TextView unreadText = holder.getView(R.id.conversation_unread);

                if (conversation.isTop()) {
                    leftItemLayout.setBackgroundColor(getContext().getResources().getColor(R.color.conversation_top_color));
                } else {
                    leftItemLayout.setBackgroundColor(Color.WHITE);
                }
                conversationIconView.setConversation(conversation);

                titleText.setText(conversation.getTitle());
                messageText.setText("");
                timelineText.setText("");

                if (conversation.getUnRead() > 0) {
                    unreadText.setVisibility(View.VISIBLE);
                    if (conversation.getUnRead() > 99) {
                        unreadText.setText("99+");
                    } else {
                        unreadText.setText("" + conversation.getUnRead());
                    }
                } else {
                    unreadText.setVisibility(View.GONE);
                }

                if (getItemDateTextSize() != 0) {
                    timelineText.setTextSize(getItemDateTextSize());
                }
                if (getItemBottomTextSize() != 0) {
                    messageText.setTextSize(getItemBottomTextSize());
                }
                if (getItemTopTextSize() != 0) {
                    titleText.setTextSize(getItemTopTextSize());
                }

                if (mOnItemClickListener != null) {
                    leftItemLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mOnItemClickListener.onItemClick(view, holder.getAdapterPosition(), conversation);
                        }
                    });
                }
                if (mOnItemLongClickListener != null) {
                    leftItemLayout.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mOnItemLongClickListener.OnItemLongClick(view, holder.getAdapterPosition(), conversation);
                            return true;
                        }
                    });
                }
            }
            break;

            case ConversationInfo.TYPE_COMMON: {
                LinearLayout leftItemLayout = holder.getView(R.id.item_left);
                ConversationIconView conversationIconView = holder.getView(R.id.conversation_icon);
                TextView titleText = holder.getView(R.id.conversation_title);
                TextView messageText = holder.getView(R.id.conversation_last_msg);
                TextView timelineText = holder.getView(R.id.conversation_time);
                TextView unreadText = holder.getView(R.id.conversation_unread);
                TextView tvDelete = holder.getView(R.id.tvDelete);

                holder.setVisible(R.id.ivTagGroup, conversation.isGroup());

                MessageInfo lastMsg = conversation.getLastMessage();
                if (lastMsg != null && lastMsg.getStatus() == MessageInfo.MSG_STATUS_REVOKE) {
                    if (lastMsg.isSelf()) {
                        lastMsg.setExtra("您撤回了一条消息");
                    } else if (lastMsg.isGroup()) {
                        String message = TUIKitConstants.covert2HTMLString(MessageInfoUtil.getTimName(lastMsg));
                        lastMsg.setExtra(message + "撤回了一条消息");
                    } else {
                        lastMsg.setExtra("对方撤回了一条消息");
                    }
                }

//        if (conversation.isTop()) {
//            leftItemLayout.setBackgroundColor(rootView.getResources().getColor(R.color.chat_bg));
//        } else {
//            leftItemLayout.setBackgroundColor(rootView.getResources().getColor(R.color.chat_bg));
//        }

                titleText.setText(conversation.getTitle());
                messageText.setText("");
                timelineText.setText("");
                if (lastMsg != null) {
                    if (lastMsg.getExtra() != null) {
                        messageText.setText(Html.fromHtml(lastMsg.getExtra().toString()));
                        messageText.setTextColor(getContext().getResources().getColor(R.color.list_bottom_text_bg));
                    }
                    timelineText.setText(DateTimeUtil.getTimeFormatText(new Date(lastMsg.getMsgTime() * 1000)));
                }

                if (conversation.getUnRead() > 0) {
                    unreadText.setVisibility(View.VISIBLE);
                    if (conversation.getUnRead() > 99) {
                        unreadText.setText("99+");
                    } else {
                        unreadText.setText("" + conversation.getUnRead());
                    }
                } else {
                    unreadText.setVisibility(View.GONE);
                }

                conversationIconView.setRadius(getItemAvatarRadius());
                if (getItemDateTextSize() != 0) {
                    timelineText.setTextSize(getItemDateTextSize());
                }
                if (getItemBottomTextSize() != 0) {
                    messageText.setTextSize(getItemBottomTextSize());
                }
                if (getItemTopTextSize() != 0) {
                    titleText.setTextSize(getItemTopTextSize());
                }
                if (!hasItemUnreadDot()) {
                    unreadText.setVisibility(View.GONE);
                }

                if (conversation.getIconUrlList() != null) {
                    conversationIconView.setConversation(conversation);
                }


                if (mOnItemClickListener != null) {
                    leftItemLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mOnItemClickListener.onItemClick(view, holder.getAdapterPosition(), conversation);
                        }
                    });
                    tvDelete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mOnItemClickListener.onDelClick(view, holder.getAdapterPosition(), conversation);
                        }
                    });
                }
                if (mOnItemLongClickListener != null) {
                    leftItemLayout.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mOnItemLongClickListener.OnItemLongClick(view, holder.getAdapterPosition(), conversation);
                            return true;
                        }
                    });
                }

            }
            break;
        }

        // holder.layoutViews(conversationInfo, holder.getAdapterPosition());
    }


//    public ConversationInfo getItem(int position) {
//        if (mDataSource.size() == 0)
//            return null;
//        return mDataSource.get(position);
//    }

//    @Override
//    public int getItemCount() {
//        return mDataSource.size();
//    }

//    @Override
//    public int getItemViewType(int position) {
//        if (mDataSource != null) {
//            ConversationInfo conversation = mDataSource.get(position);
//            return conversation.getType();
//        }
//        return 1;
//    }

    public void addItem(int position, ConversationInfo info) {
        mDataSource.add(position, info);
        notifyItemInserted(position);
        notifyDataSetChanged();
    }

    public void removeItem(int position) {
        mDataSource.remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public void notifyDataSourceChanged(String info) {
        for (int i = 0; i < mDataSource.size(); i++) {
            if (TextUtils.equals(info, mDataSource.get(i).getConversationId())) {
                notifyItemChanged(i);
                return;
            }
        }
    }

    public void setItemTopTextSize(int size) {
        mTopTextSize = size;
    }

    public int getItemTopTextSize() {
        return mTopTextSize;
    }

    public void setItemBottomTextSize(int size) {
        mBottomTextSize = size;
    }

    public int getItemBottomTextSize() {
        return mBottomTextSize;
    }

    public void setItemDateTextSize(int size) {
        mDateTextSize = size;
    }

    public int getItemDateTextSize() {
        return mDateTextSize;
    }

    public void setItemAvatarRadius(int radius) {
        mItemAvatarRadius = radius;
    }

    public int getItemAvatarRadius() {
        return mItemAvatarRadius;
    }

    public void disableItemUnreadDot(boolean flag) {
        mHasShowUnreadDot = !flag;
    }

    public boolean hasItemUnreadDot() {
        return mHasShowUnreadDot;
    }


}
